{*Smarty*}
<html>
<head>
  <title>Log in!</title>
  <link rel="stylesheet" type="text/css" href="/default.css" />
  <script type="text/javascript" src="/js/prototype.js"></script>
  <script type="text/javascript">
	{* обязательно необходимо воспользоваться smarty функцией literal,
		что бы было возможно выполнить функции javascript-а, которые содержат ‘{‘ и ‘}’
	*}
	{literal}	
	function onSubmit()
	{
		loginAjaxRequest();
	    return false; 
	}
	
	function handleSuccess(transport)
	{
	  var data = transport.responseText.evalJSON(true);
	  
	  if (data.loggedin)
	  {
	  	 $('login_form').style.visibility = "collapse";
	  	 $('welcome_text').style.visibility = "visible";
	  }
	  else 
	  {
	  	 $('login_form').style.visibility = "visible";
	  	 $('welcome_text').style.visibility = "collapse";
	  
	  	  Object.keys(data.user).each( function(key) 
	  		{
	  			var error_text = data.user[key].error;
	  			var error_control = $(key + '_error');
	  			if (error_text !== undefined)
		  			error_control.textContent = error_text;
		  		else 
		  			error_control.textContent = "";
	  		});
	  }
	}
	function handleFailure(transport)
	{
	  
	}
	function handleComplete(transport)
	{
	  
	}
	
	function loginAjaxRequest()
	{
		
	  $('login_form').request( {
	        onSuccess : handleSuccess,
			onFailure : handleFailure,
			onComplete : handleComplete
	      } );
	}
	{/literal}
  </script>
</head>
<body>

<form 
	style="visibility:{if $loggedin}collapse{else}visible{/if};"
	id="login_form" 
	method="post" 
	action="/reg/login" 
	onsubmit="return onSubmit()">
  <div class="form">
    {foreach key=key item=field from=$user}
      <div class="field">
        <div class="key">{$field.label}: </div>
        <div class="value">
          {if $field.type == 'password'}
            <input type="password" name="{$key}" id="{$key}_value" value="{$field.value}"/>
          {else}
            <input type="text" name="{$key}" id="{$key}_value" value="{$field.value}"/>
          {/if}
        </div>
        <div class="error" id="{$key}_error">{$field.error}</div>
      </div>
    {/foreach}
    
    <div class="field">
      <div class="key"></div>
      <div class="value"><input class="button" type="submit" value="Log in!" /></div>
    </div>
  </div>
</form>

<div id="welcome_text" style="visibility:{if $loggedin}visible{else}collapse{/if};">
	Welcome, {$name}! <a href="/reg/logout">Log out. </a>
</div>


</body>
</html>